package com.njegovanobradovic.icet.pages;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

import com.njegovanobradovic.icet.facade.Controller;
import com.njegovanobradovic.icet.metamodel.Chat;
import com.njegovanobradovic.icet.metamodel.Message;
import com.njegovanobradovic.icet.metamodel.User;
import com.njegovanobradovic.icet.util.translate.GoogleAPI;
import com.njegovanobradovic.icet.util.translate.GoogleAPIException;
import com.njegovanobradovic.icet.util.translate.Language;
import com.njegovanobradovic.icet.util.translate.Translate;

@Import(library = "js/AJAXPeriodicUpdate.js")
public class Chating {
	@Persist
	private Chat chat;
	
	@Property
	private Message message;
	@Property
	private User user;

	@Inject
	private Controller controller;


	@Validate(value = "required")
	private String content;
	
	public Chat getChat() {
		return chat;
	}

	public void setChat(Chat chat) {
		this.chat = chat;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() {
		return content;
	}

	public Object onActivate() {
		return controller.isLoggedIn() ? null : Signin.class;
	}

	@OnEvent(value = "submit", component = "addMessageForm")
	public void onFormSubmit() {
		User u1,u2;
		if (controller.getLoggedUser().equals(controller.getChatUsers(chat).get(0))) {
			u1=controller.getChatUsers(chat).get(0);
			u2=controller.getChatUsers(chat).get(1);
		} else {
			u2=controller.getChatUsers(chat).get(0);
			u1=controller.getChatUsers(chat).get(1);
		}
		String text = controller.translate(content, u1.getLanguage(), u2.getLanguage());
		controller.addMessageToChat(controller.getLoggedUser(),text, getChat());
	}

	
	
	@InjectComponent
	private Zone listZone;
	@Inject
	private JavaScriptSupport javaScriptSupport;
	
	public void setupRender() {
		javaScriptSupport.addInitializerCall("periodicTimeZoneUpdater", new JSONObject());
	}
	
	Object onRefreshTimeZone() {
		return !controller.getRequest().getSession(true).isInvalidated() ? listZone.getBody() : controller.userLogout();
	}
	
	public List<Message> getMessages() {
		List<Message> messages = controller.getChatMessages(getChat());
		Comparator<Message> com = Collections.reverseOrder();
		Collections.sort(messages, com);
		return messages;
	}
	
	public List<User> getUsers(){
		return getChat().getUsers();
	}
	
	
	@InjectPage
	private Details detailsPage;
	@OnEvent(component = "detailsLink")
	Object onShowDetails(String username) {
		detailsPage.setUser(controller.getUser(username));
		return detailsPage;
	}

}
